package com.kmzx.one.project.service.impl;

import com.github.pagehelper.Page;
import com.kmzx.one.project.entity.Brand;
import com.kmzx.one.project.entity.RankTab;
import com.kmzx.one.project.mapper.BrandMapper;
import com.kmzx.one.project.mapper.CustomerMapper;
import com.kmzx.one.project.mapper.FuncMapper;
import com.kmzx.one.project.mapper.ProdBaseMapper;
import com.kmzx.one.project.mapper.ProductMapper;
import com.kmzx.one.project.mapper.RankTabMapper;
import com.kmzx.one.project.mapper.UserRankMapper;
import com.kmzx.one.project.others.dso.ProdCatePriceDso;
import com.kmzx.one.project.others.dso.ProdJoinDiscount;
import com.kmzx.one.project.others.dso.UserRankDso;
import com.kmzx.one.project.others.dto.BrandInfo;
import com.kmzx.one.project.others.dto.FastProdInfo;
import com.kmzx.one.project.others.dto.GreatBrand;
import com.kmzx.one.project.others.dto.GreatBrandMain;
import com.kmzx.one.project.others.dto.ProdOutInfo;
import com.kmzx.one.project.others.dto.StraightInfo;
import com.kmzx.one.project.others.dto.UserRankInfo;
import com.kmzx.one.project.service.FuncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class FuncServiceImpl implements FuncService {

    @Autowired
    BrandMapper brandMapper;

    @Autowired
    FuncMapper funcMapper;

    @Autowired
    ProdBaseMapper prodBaseMapper;

    @Autowired
    UserRankMapper userRankMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private RankTabMapper rankTabMapper;

    public String getRankId(String userId) {
        List<UserRankDso> dsos = userRankMapper.selectAllRankTabs();
        String rankId = dsos.get(0).getRankId();
        if (userId != null && !userId.equals("")) {
            String s = userId.toLowerCase();
            if (!"null".equals(s)) {
                UserRankInfo info = userRankMapper.selectUserRankTabs(userId);
                String rankId1 = info.getRankId();
                rankId = rankId1;
            }
        }

        return rankId;
    }

    @Override
    public List<GreatBrand> findGreatBrand(String userId) throws Exception {

        String rankId = getRankId(userId);
//        String rankId = (String) ServletUtils.getAttr("level");
        boolean specific = false;
        List<String> specUsers = customerMapper.findSpecificUser();
        if (!CollectionUtils.isEmpty(specUsers) && (userId != null && userId != "")) {
            if (specUsers.contains(userId)) {
                specific = true;
            }
        }

        List<BrandInfo> singleBrandAll = funcMapper.selectSingleBrandAll();

        RankTab rankTab = rankTabMapper.selectByPrimaryKey(rankId);
        Integer rankNum = rankTab.getRankNumber();

        if (singleBrandAll != null && singleBrandAll.size() > 0) {
            List<String> funcIds = singleBrandAll.stream().map(bl -> bl.getBrandId()).collect(Collectors.toList());
            List<BrandInfo> brandList = funcMapper.selectAllBrand2(rankId,funcIds);

            List<String> prodIdList = brandList.stream().map(bl -> bl.getProdId()).collect(Collectors.toList());
            if (prodIdList != null) {

            } else {
                prodIdList = Collections.emptyList();
            }
            List<ProdCatePriceDso> prodPriceList = productMapper.selectProdGroupPriceList(prodIdList);
            // 所有的商品打折活动,包括所有的商品
            List<ProdJoinDiscount> discounts = productMapper.selectAllProductJoinDiscount();
            List<StraightInfo> infoList = productMapper.selectAllStraightInfo();
            List<FastProdInfo> fastProdList = new ArrayList<>();
            boolean had = false;

            for (StraightInfo info : infoList) {
                String id = info.getId();
                had = true;
                Page<FastProdInfo> infos = productMapper.selectFastProdInfoPaged(id);
                fastProdList.addAll(infos.getResult());
            }


            List<FastProdInfo> infos = productMapper.selectFastProdInfoPagedList(prodIdList);

            boolean fasts = false;
            if (infos != null && infos.size() > 0) {
                fasts = true;
            }


            if (prodPriceList != null && prodPriceList.size() > 0) {
                Map<String, Boolean> houseMap = new HashMap<>();
                Map<String, BigDecimal> priceMap = new HashMap<>();
                Map<String, BigDecimal> specificPriceMap = new HashMap<>();
                for (ProdCatePriceDso priceDso : prodPriceList) {
                    String productId = priceDso.getProductId();
                    priceMap.put(productId, priceDso.getMiniPrice());
                    specificPriceMap.put(productId, priceDso.getMiniCost());
                    Integer behalf = priceDso.getBehalf();
                    if (behalf != null) {
                        if (behalf == 0) {
                            houseMap.put(productId, true);
                        } else {
                            houseMap.put(productId, priceDso.getNomNumbers() > 0);
                        }
                    } else {
                        houseMap.put(productId, priceDso.getNomNumbers() > 0);
                    }
                }
                Map<String, List<BrandInfo>> brandGroup = brandList.stream().collect(Collectors.groupingBy(BrandInfo::getBrandId));
                ArrayList<GreatBrand> brands = new ArrayList<>(brandList.size());

                for (BrandInfo bi : singleBrandAll){
                    GreatBrand greatBrand = new GreatBrand();
                    List<BrandInfo> brandInfos = brandGroup.get(bi.getBrandId());

                    greatBrand.setBrandId(bi.getBrandId()).setBrandName(bi.getBrandName());
                    greatBrand.setBrandPic(bi.getBrandPic());
                    greatBrand.setSorts(bi.getSorts());

                    if(brandInfos!=null && brandInfos.size()>0){
                        int length = brandInfos.size();
                        List<ProdOutInfo> prodList = new ArrayList<>(length);
                        brandInfos.sort(Comparator.comparing(BrandInfo::getSecSorts).thenComparing(BrandInfo::getSorts));


                        for (BrandInfo brandInfo : brandInfos) {

                            ProdOutInfo prodOutInfo = new ProdOutInfo();
                            prodOutInfo.setProdName(brandInfo.getProdName()).setProdImg(brandInfo.getProdPic());
                            String prodAbbrName = brandInfo.getProdAbbrName();
                            if (prodAbbrName != null && !"".equals(prodAbbrName)) {
                                prodOutInfo.setProdAbbrName(prodAbbrName);
                            } else {
                                prodOutInfo.setProdAbbrName(brandInfo.getProdName());
                            }

                            prodOutInfo.setSorts(brandInfo.getPwcSorts());
                            BigDecimal price = priceMap.get(brandInfo.getProdId());
                            if (price != null) {
                                prodOutInfo.setProdPrice(price);
                                // 活动价 ,要改
                                prodOutInfo.setCurrentPrice(price);
                            } else {
                                BigDecimal pricel = productMapper.selectProdGroupPrice(brandInfo.getProdId());
                                prodOutInfo.setProdPrice(pricel);
                                prodOutInfo.setCurrentPrice(pricel);
                            }
                            prodOutInfo.setProdId(brandInfo.getProdId());
                            Boolean aBoolean = houseMap.get(brandInfo.getProdId());
                            if (aBoolean != null) {
                                prodOutInfo.setHas(houseMap.get(brandInfo.getProdId()));
                            } else {
                                prodOutInfo.setHas(false);
                            }

                            if (fasts) {
                                prodOutInfo.setKinds(2);
                                for (FastProdInfo info : infos) {
                                    if (brandInfo.getProdId().equals(info.getProdId()) && info.getRankNum() < rankNum) {
                                        prodOutInfo.setCurrentPrice(info.getCurrentPrice());
                                        prodOutInfo.setFastIcon(info.getFastIcon());
                                    }
                                }
                            }


                            if (!CollectionUtils.isEmpty(discounts)) {
                                Map<String, List<ProdJoinDiscount>> listMap = discounts.stream().collect(Collectors.groupingBy(ProdJoinDiscount::getProductId));

                                if (prodOutInfo.getKinds() != 2) {
                                    String prodId = prodOutInfo.getProdId();
                                    List<ProdJoinDiscount> joinDiscounts = listMap.get(prodId);
                                    if (joinDiscounts != null) {
                                        for (ProdJoinDiscount joinDiscount : joinDiscounts) {
                                            Integer discountCate = joinDiscount.getDiscountCate();
                                            if (discountCate == 0) {
                                                joinDiscount.setCurrentPrice(prodOutInfo.getProdPrice().multiply(joinDiscount.getDiscountNum()));
                                            } else if (discountCate == 1 || discountCate == 2) {
                                                joinDiscount.setCurrentPrice(joinDiscount.getDiscountNum());
                                            } else if (discountCate == 3) {
                                                joinDiscount.setCurrentPrice(joinDiscount.getDiscountNum());
                                            }
                                        }
                                        joinDiscounts.sort(Comparator.comparing(ProdJoinDiscount::getCurrentPrice));
                                        Integer discountCate = joinDiscounts.get(0).getDiscountCate();
                                        prodOutInfo.setKinds(1);
                                        // 0折扣  1 一口价
                                        if (discountCate == 0) {
                                            prodOutInfo.setCurrentPrice(prodOutInfo.getProdPrice().multiply(joinDiscounts.get(0).getDiscountNum()));
                                        } else if (discountCate == 1) {
                                            prodOutInfo.setCurrentPrice(joinDiscounts.get(0).getDiscountNum());
                                        } else if (discountCate == 3) {
                                            prodOutInfo.setCurrentPrice(joinDiscounts.get(0).getDiscountNum());
                                        }
                                        prodOutInfo.setBeauty(prodOutInfo.getCurrentPrice());
                                        prodOutInfo.setDiscountImg(joinDiscounts.get(0).getDiscountImg());
                                    }
                                }
                            }

                            if (prodOutInfo.getProdId() != null) {
                                prodList.add(prodOutInfo);
                            }
                            if (specific) {
                                BigDecimal spePrice = specificPriceMap.get(brandInfo.getProdId());
                                if (spePrice != null) {
                                    prodOutInfo.setProdPrice(spePrice);
                                    // 活动价 ,要改
                                    prodOutInfo.setCurrentPrice(spePrice);
                                } else {
                                    BigDecimal pricel = productMapper.findProductSpecificMinPrice(brandInfo.getProdId());
                                    prodOutInfo.setProdPrice(pricel);
                                    prodOutInfo.setCurrentPrice(pricel);
                                }
                            }
                        }
                        if (!specific) {
                            prodList = switchToFastProd(prodList, userId, discounts);
                            if (rankNum > 0) {
                                for (ProdOutInfo prodOutInfo : prodList) {
                                    if (prodOutInfo.getRankNum() < rankNum && !prodOutInfo.isOneValue()) {
                                        prodOutInfo.setRankIcon(rankTab.getRankIcon());
                                        // 直升商品
                                        if (prodOutInfo.getKinds() == 2) {
                                            prodOutInfo.setRankPrice(prodOutInfo.getProdPrice().multiply(rankTab.getBigRankRate()));
                                        } else {
                                            prodOutInfo.setRankPrice(prodOutInfo.getCurrentPrice().multiply(rankTab.getBigRankRate()));
                                        }
                                    }
                                }
                            }
                        }
//                        if (prodList != null && prodList.size() > 0) {
//                            prodList.sort(Comparator.comparing(ProdOutInfo::getSorts));
//                        }
                        greatBrand.setProdList(prodList);
                    }
                    brands.add(greatBrand);
                }
                if (brands != null && brands.size() > 0) {
                    brands.sort(Comparator.comparing(GreatBrand::getSorts));
                }
                return brands;
            } else {
                ArrayList<GreatBrand> brands = new ArrayList<>(singleBrandAll.size());

                for (BrandInfo s : singleBrandAll) {
                    GreatBrand greatBrand = new GreatBrand();

                    greatBrand.setBrandId(s.getBrandId()).setBrandName(s.getBrandName());
                    greatBrand.setBrandPic(s.getBrandPic());
                    greatBrand.setSorts(s.getSorts());

                    brands.add(greatBrand);
                }
                if (brands != null && brands.size() > 0) {
                    brands.sort(Comparator.comparing(GreatBrand::getSorts));
                }
                return brands;
            }
        } else {
            return null;
        }
    }

    @Override
    public GreatBrandMain findBrandMainInfo(String brandId, String userId) throws Exception {

        Brand brand = funcMapper.selectById(brandId);
        List<String> list = funcMapper.selectBrandPics(brandId);
        System.out.println("显示的数据图片为:" + list);
        GreatBrandMain main = new GreatBrandMain();
        main.setBrandId(brandId).setBrandName(brand.getCategoryName()).setCarouselsImg(list);
        main.setDescription(brand.getDescription());
//        List<ProdOutInfo> infos = findProdByCateIdPager(brandId, userId, 1, 1000);
//        main.setProdList(infos);

        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                String pic = list.get(i);
                int dot = pic.lastIndexOf(".");
                int subLine = pic.lastIndexOf("_");
                String[] xes = pic.substring(subLine + 1, dot).split("×");
                Integer width = Integer.valueOf(xes[0]);
                Integer height = Integer.valueOf(xes[1]);
                if (main.getMaxWidth() < width) {
                    main.setMaxWidth(width);
                }
                if (main.getMaxHeight() < height) {
                    main.setMaxHeight(height);
                }
            }
        }

        return main;
    }


    @Override
    public List<GreatBrand> findProdOfCateInfo2(String userId, Integer funcId) throws Exception {

//         selectAllBrandCate
        String rankId = getRankId(userId);
//        String rankId = (String) ServletUtils.getAttr("level");
        boolean specific = false;
        List<String> specUsers = customerMapper.findSpecificUser();
        if (!CollectionUtils.isEmpty(specUsers) && (userId != null && userId != "")) {
            if (specUsers.contains(userId)) {
                specific = true;
            }
        }
        List<BrandInfo> brandList = funcMapper.selectAllBrandCate(rankId, funcId);
        RankTab rankTab = rankTabMapper.selectByPrimaryKey(rankId);
        Integer rankNum = rankTab.getRankNumber();


        if (brandList != null && brandList.size() > 0) {
            List<String> prodIdList = brandList.stream().map(bl -> bl.getProdId()).collect(Collectors.toList());

            List<ProdCatePriceDso> prodPriceList = productMapper.selectProdGroupPriceList(prodIdList);
            // 所有的商品打折活动,包括所有的商品
            List<ProdJoinDiscount> discounts = productMapper.selectAllProductJoinDiscount();
            List<StraightInfo> infoList = productMapper.selectAllStraightInfo();
            List<FastProdInfo> fastProdList = new ArrayList<>();
            boolean had = false;

            for (StraightInfo info : infoList) {
                String id = info.getId();
                had = true;
                Page<FastProdInfo> infos = productMapper.selectFastProdInfoPaged(id);
                fastProdList.addAll(infos.getResult());
            }


            List<FastProdInfo> infos = productMapper.selectFastProdInfoPagedList(prodIdList);

            boolean fasts = false;
            if (infos != null && infos.size() > 0) {
                fasts = true;
            }


            if (prodPriceList != null && prodPriceList.size() > 0) {
                Map<String, Boolean> houseMap = new HashMap<>();
                Map<String, BigDecimal> priceMap = new HashMap<>();
                Map<String, BigDecimal> specificPriceMap = new HashMap<>();
                for (ProdCatePriceDso priceDso : prodPriceList) {
                    String productId = priceDso.getProductId();
                    priceMap.put(productId, priceDso.getMiniPrice());
                    specificPriceMap.put(productId, priceDso.getMiniCost());
                    Integer behalf = priceDso.getBehalf();
                    if (behalf != null) {
                        if (behalf == 0) {
                            houseMap.put(productId, true);
                        } else {
                            houseMap.put(productId, priceDso.getNomNumbers() > 0);
                        }
                    } else {
                        houseMap.put(productId, priceDso.getNomNumbers() > 0);
                    }
                }
                Map<String, List<BrandInfo>> brandGroup = brandList.stream().collect(Collectors.groupingBy(BrandInfo::getCateId));
                ArrayList<GreatBrand> brands = new ArrayList<>(brandGroup.size());

                Set<String> brandSet = brandGroup.keySet();

                for (String s : brandSet) {
                    GreatBrand greatBrand = new GreatBrand();
                    List<BrandInfo> brandInfos = brandGroup.get(s);
                    if(brandInfos!=null && brandInfos.size()>0){
//                        brandInfos.sort(Comparator.comparing(BrandInfo::getSecSorts).thenComparing(BrandInfo::getSorts));
                        greatBrand.setBrandId(s).setBrandName(brandInfos.get(0).getBrandName());
                        greatBrand.setBrandPic(brandInfos.get(0).getBrandPic());
                        greatBrand.setSorts(brandInfos.get(0).getSorts());
                        greatBrand.setCateName(brandInfos.get(0).getCateName());

                        int length = brandInfos.size();
                        List<ProdOutInfo> prodList = new ArrayList<>(length);

                        for (BrandInfo brandInfo : brandInfos) {

                            ProdOutInfo prodOutInfo = new ProdOutInfo();
                            prodOutInfo.setProdName(brandInfo.getProdName()).setProdImg(brandInfo.getProdPic());
                            String prodAbbrName = brandInfo.getProdAbbrName();
                            if (prodAbbrName != null && !"".equals(prodAbbrName)) {
                                prodOutInfo.setProdAbbrName(prodAbbrName);
                            } else {
                                prodOutInfo.setProdAbbrName(brandInfo.getProdName());
                            }

                            prodOutInfo.setSorts(brandInfo.getPwcSorts());
                            BigDecimal price = priceMap.get(brandInfo.getProdId());
                            if (price != null) {
                                prodOutInfo.setProdPrice(price);
                                // 活动价 ,要改
                                prodOutInfo.setCurrentPrice(price);
                            } else {
                                BigDecimal pricel = productMapper.selectProdGroupPrice(brandInfo.getProdId());
                                prodOutInfo.setProdPrice(pricel);
                                prodOutInfo.setCurrentPrice(pricel);
                            }
                            prodOutInfo.setProdId(brandInfo.getProdId());
                            Boolean aBoolean = houseMap.get(brandInfo.getProdId());
                            if (aBoolean != null) {
                                prodOutInfo.setHas(houseMap.get(brandInfo.getProdId()));
                            } else {
                                prodOutInfo.setHas(false);
                            }

                            if (fasts) {
                                prodOutInfo.setKinds(2);
                                for (FastProdInfo info : infos) {
                                    if (brandInfo.getProdId().equals(info.getProdId()) && info.getRankNum() < rankNum) {
                                        prodOutInfo.setCurrentPrice(info.getCurrentPrice());
                                        prodOutInfo.setFastIcon(info.getFastIcon());
                                    }
                                }
                            }


                            if (!CollectionUtils.isEmpty(discounts)) {
                                Map<String, List<ProdJoinDiscount>> listMap = discounts.stream().collect(Collectors.groupingBy(ProdJoinDiscount::getProductId));

                                if (prodOutInfo.getKinds() != 2) {
                                    String prodId = prodOutInfo.getProdId();
                                    List<ProdJoinDiscount> joinDiscounts = listMap.get(prodId);
                                    if (joinDiscounts != null) {
                                        for (ProdJoinDiscount joinDiscount : joinDiscounts) {
                                            Integer discountCate = joinDiscount.getDiscountCate();
                                            if (discountCate == 0) {
                                                joinDiscount.setCurrentPrice(prodOutInfo.getProdPrice().multiply(joinDiscount.getDiscountNum()));
                                            } else if (discountCate == 1 || discountCate == 2) {
                                                joinDiscount.setCurrentPrice(joinDiscount.getDiscountNum());
                                            } else if (discountCate == 3) {
                                                joinDiscount.setCurrentPrice(joinDiscount.getDiscountNum());
                                            }
                                        }
                                        joinDiscounts.sort(Comparator.comparing(ProdJoinDiscount::getCurrentPrice));
                                        Integer discountCate = joinDiscounts.get(0).getDiscountCate();
                                        prodOutInfo.setKinds(1);
                                        // 0折扣  1 一口价
                                        if (discountCate == 0) {
                                            prodOutInfo.setCurrentPrice(prodOutInfo.getProdPrice().multiply(joinDiscounts.get(0).getDiscountNum()));
                                        } else if (discountCate == 1) {
                                            prodOutInfo.setCurrentPrice(joinDiscounts.get(0).getDiscountNum());
                                        } else if (discountCate == 3) {
                                            prodOutInfo.setCurrentPrice(joinDiscounts.get(0).getDiscountNum());
                                        }
                                        prodOutInfo.setBeauty(prodOutInfo.getCurrentPrice());
                                        prodOutInfo.setDiscountImg(joinDiscounts.get(0).getDiscountImg());
                                    }
                                }
                            }

                            if (prodOutInfo.getProdId() != null) {
                                prodList.add(prodOutInfo);
                            }
                            if (specific) {
                                BigDecimal spePrice = specificPriceMap.get(brandInfo.getProdId());
                                if (spePrice != null) {
                                    prodOutInfo.setProdPrice(spePrice);
                                    // 活动价 ,要改
                                    prodOutInfo.setCurrentPrice(spePrice);
                                } else {
                                    BigDecimal pricel = productMapper.findProductSpecificMinPrice(brandInfo.getProdId());
                                    prodOutInfo.setProdPrice(pricel);
                                    prodOutInfo.setCurrentPrice(pricel);
                                }
                            }
                        }
                        if (!specific) {
                            prodList = switchToFastProd(prodList, userId, discounts);
                            if (rankNum > 0) {
                                for (ProdOutInfo prodOutInfo : prodList) {
                                    if (prodOutInfo.getRankNum() < rankNum && !prodOutInfo.isOneValue()) {
                                        prodOutInfo.setRankIcon(rankTab.getRankIcon());
                                        // 直升商品
                                        if (prodOutInfo.getKinds() == 2) {
                                            prodOutInfo.setRankPrice(prodOutInfo.getProdPrice().multiply(rankTab.getBigRankRate()));
                                        } else {
                                            prodOutInfo.setRankPrice(prodOutInfo.getCurrentPrice().multiply(rankTab.getBigRankRate()));
                                        }
                                    }
                                }
                            }
                        }
                        if (prodList != null && prodList.size() > 0) {
                            prodList.sort(Comparator.comparing(ProdOutInfo::getSorts));
                        }
                        greatBrand.setProdList(prodList);
                        brands.add(greatBrand);
                    }
                }
                if (brands != null && brands.size() > 0) {
                    brands.sort(Comparator.comparing(GreatBrand::getSorts));

                    ArrayList<ProdOutInfo> endProdList = new ArrayList<>();
                    for (GreatBrand brand : brands) {
                        List<ProdOutInfo> prodList = brand.getProdList();
                        for (ProdOutInfo prodOutInfo : prodList) {
                            endProdList.add(prodOutInfo);
                        }
                    }
                    if (endProdList != null && endProdList.size() > 0) {
                        endProdList.sort(Comparator.comparing(ProdOutInfo::getSorts));
                    }

                    GreatBrand greatBrand = brands.get(0);
                    greatBrand.setProdList(endProdList);
                    greatBrand.setCateName("");
                    brands.clear();
                    brands.add(greatBrand);

                }
                return brands;
            }
            return null;
        } else {
            return null;
        }
    }


    public List<ProdOutInfo> resolveProdOutInfo(List<ProdOutInfo> end, String userId, UserRankInfo userRankInfo, int rankNum) {
        if (end != null && end.size() > 0) {
            {

                List<String> prodIds = end.stream().map(qq -> qq.getProdId()).collect(Collectors.toList());
                List<ProdCatePriceDso> priceDsos = productMapper.selectProdGroupPriceList(prodIds);


                // 所有的商品打折活动,包括所有的商品
                List<ProdJoinDiscount> discounts = productMapper.selectAllProductJoinDiscount();

                boolean specific = false;
                List<String> specUsers = customerMapper.findSpecificUser();
                if (!CollectionUtils.isEmpty(specUsers) && (userId != null && userId != "")) {
                    if (specUsers.contains(userId)) {
                        specific = true;
                    }
                }
                if (!CollectionUtils.isEmpty(priceDsos)) {
                    Map<String, List<ProdCatePriceDso>> map = priceDsos.stream().collect(Collectors.groupingBy(ProdCatePriceDso::getProductId));


                    if (specific) {
                        for (ProdOutInfo prod : end) {
                            List<ProdCatePriceDso> prodCatePriceDsos = map.get(prod.getProdId());
                            if (prodCatePriceDsos != null) {
                                ProdCatePriceDso pcds = prodCatePriceDsos.get(0);
                                BigDecimal miniCost = pcds.getMiniCost();
                                miniCost = miniCost.multiply(BigDecimal.valueOf(pcds.getProdNum()));
//                                BigDecimal miniCost = prodCatePriceDsos.get(0).getMiniCost();
                                prod.setProdPrice(miniCost);
                                prod.setBeauty(miniCost);
                                prod.setCurrentPrice(miniCost);
                                Integer behalf = pcds.getBehalf();
                                if (behalf != null) {
                                    if (behalf == 0) {
                                        prod.setHas(true);
                                    } else {
                                        prod.setHas(pcds.getNomNumbers() > 0);
                                    }
                                } else {
                                    prod.setHas(pcds.getNomNumbers() > 0);
                                }
                            } else {
                                prod.setCurrentPrice(BigDecimal.ONE);
                                prod.setProdPrice(BigDecimal.ONE);
                                prod.setBeauty(BigDecimal.ONE);
                            }
                        }
                    } else {
                        for (ProdOutInfo prod : end) {
                            List<ProdCatePriceDso> prodCatePriceDsos = map.get(prod.getProdId());
                            if (prodCatePriceDsos != null) {
                                ProdCatePriceDso pcds = prodCatePriceDsos.get(0);
                                BigDecimal miniPrice = pcds.getMiniPrice();

                                Integer behalf = pcds.getBehalf();
                                if (behalf != null) {
                                    if (behalf == 0) {
                                        prod.setHas(true);
                                    } else {
                                        prod.setHas(pcds.getNomNumbers() > 0);
                                    }
                                } else {
                                    prod.setHas(pcds.getNomNumbers() > 0);
                                }
                                prod.setCurrentPrice(miniPrice);
                                prod.setProdPrice(miniPrice);
                                prod.setBeauty(miniPrice);
                            } else {
                                BigDecimal price = productMapper.selectProdGroupPrice(prod.getProdId());
                                if (price == null) {
                                    prod.setCurrentPrice(BigDecimal.ONE);
                                    prod.setProdPrice(BigDecimal.ONE);
                                    prod.setBeauty(BigDecimal.ONE);
                                } else {
                                    prod.setCurrentPrice(price);
                                    prod.setBeauty(price);
                                    prod.setProdPrice(price);
                                }
                            }
                        }
                        end = switchToFastProd(end, userId, discounts);
                        // 直升商品
                        if (rankNum > 0) {
                            for (ProdOutInfo prodOutInfo : end) {

                                if (!prodOutInfo.isOneValue() && prodOutInfo.getRankNum() < rankNum) {
                                    prodOutInfo.setRankIcon(userRankInfo.getPriceIcon());
                                    // 直升商品
                                    if (prodOutInfo.getKinds() == 2) {
                                        prodOutInfo.setRankPrice(prodOutInfo.getProdPrice().multiply(userRankInfo.getBigRankRate()));
                                    } else {
                                        prodOutInfo.setRankPrice(prodOutInfo.getCurrentPrice().multiply(userRankInfo.getBigRankRate()));
                                    }
                                }
                            }
                        }
                    }
                }
                return end;
            }
        } else {
            return Collections.emptyList();
        }
    }


    public List<ProdOutInfo> switchToFastProd(List<ProdOutInfo> end, String userId, List<ProdJoinDiscount> discounts) {
        //
        List<StraightInfo> infoList = productMapper.selectAllStraightInfo();
        if (infoList == null) {
            if (!CollectionUtils.isEmpty(discounts)) {
                Map<String, List<ProdJoinDiscount>> listMap = discounts.stream().collect(Collectors.groupingBy(ProdJoinDiscount::getProductId));

                for (ProdOutInfo prod : end) {
                    String prodId = prod.getProdId();
                    List<ProdJoinDiscount> joinDiscounts = listMap.get(prodId);
                    if (joinDiscounts != null) {
                        prod.setKinds(1);

                        for (ProdJoinDiscount joinDiscount : joinDiscounts) {
                            Integer discountCate = joinDiscount.getDiscountCate();
                            if (discountCate == 0) {
                                joinDiscount.setCurrentPrice(prod.getProdPrice().multiply(joinDiscount.getDiscountNum()));
                            } else if (discountCate == 1 || discountCate == 2) {
                                joinDiscount.setCurrentPrice(joinDiscount.getDiscountNum());
                            } else if (discountCate == 3) {
                                joinDiscount.setCurrentPrice(joinDiscount.getDiscountNum());
                            }
                        }
                        joinDiscounts.sort(Comparator.comparing(ProdJoinDiscount::getCurrentPrice));
                        Integer discountCate = joinDiscounts.get(0).getDiscountCate();
                        // 0折扣种类1固定金额种类
                        if (discountCate == 0) {
                            prod.setCurrentPrice(prod.getProdPrice().multiply(joinDiscounts.get(0).getDiscountNum()));
                        } else if (discountCate == 1) {
                            prod.setCurrentPrice(joinDiscounts.get(0).getDiscountNum());
                        } else if (discountCate == 3) {
                            prod.setCurrentPrice(joinDiscounts.get(0).getDiscountNum());
                        }
                        prod.setBeauty(prod.getCurrentPrice());
                        prod.setDiscountImg(joinDiscounts.get(0).getDiscountImg());
                        Integer special = joinDiscounts.get(0).getSpecial();
                        if (special != null && special == 0) {
                            if (discountCate == 2) {
                                prod.setCurrentPrice(joinDiscounts.get(0).getDiscountNum());
                                prod.setOneValue(true);
                            }
                        }
                    }
                }
            }
            return end;
        }
        if (infoList != null && infoList.size() > 0) {
            UserRankInfo userRankInfo = userRankMapper.selectUserRankTabs(userId);
            boolean had = false;
            if (userRankInfo != null) {

                List<FastProdInfo> fastProdList = new ArrayList<>();
                for (StraightInfo info : infoList) {
                    String id = info.getId();
                    had = true;
                    Page<FastProdInfo> infos = productMapper.selectFastProdInfoPaged(id);
                    fastProdList.addAll(infos.getResult());
                }
                if (had) {
                    for (ProdOutInfo prodOutInfo : end) {
                        for (FastProdInfo fastProdInfo : fastProdList) {
                            if (prodOutInfo.getProdId().equals(fastProdInfo.getProdId())) {
                                prodOutInfo.setFastIcon(fastProdInfo.getFastIcon());
                                prodOutInfo.setCurrentPrice(fastProdInfo.getCurrentPrice());
                                prodOutInfo.setBeauty(fastProdInfo.getBeauty());
                                prodOutInfo.setKinds(2);
                                prodOutInfo.setRankNum(fastProdInfo.getRankNum());
                            }
                        }
                    }
                }
            } else {
                had = true;
                List<FastProdInfo> fastProdList = new ArrayList<>();
                for (StraightInfo info : infoList) {
                    String id = info.getId();
                    Page<FastProdInfo> infos = productMapper.selectFastProdInfoPaged(id);
                    fastProdList.addAll(infos.getResult());
                }
                if (had) {
                    for (ProdOutInfo prodOutInfo : end) {
                        for (FastProdInfo fastProdInfo : fastProdList) {
                            if (prodOutInfo.getProdId().equals(fastProdInfo.getProdId())) {
                                prodOutInfo.setFastIcon(fastProdInfo.getFastIcon());
                                prodOutInfo.setCurrentPrice(fastProdInfo.getCurrentPrice());
                                prodOutInfo.setBeauty(fastProdInfo.getBeauty());
                                prodOutInfo.setKinds(2);
                                prodOutInfo.setRankNum(fastProdInfo.getRankNum());
                            }
                        }
                    }
                }
            }

            if (!CollectionUtils.isEmpty(discounts)) {
                Map<String, List<ProdJoinDiscount>> listMap = discounts.stream().collect(Collectors.groupingBy(ProdJoinDiscount::getProductId));

                for (ProdOutInfo prod : end) {
                    if (prod.getKinds() != 2) {
                        String prodId = prod.getProdId();
                        List<ProdJoinDiscount> joinDiscounts = listMap.get(prodId);
                        if (joinDiscounts != null) {
                            for (ProdJoinDiscount joinDiscount : joinDiscounts) {
                                Integer discountCate = joinDiscount.getDiscountCate();
                                if (discountCate == 0) {
                                    joinDiscount.setCurrentPrice(prod.getProdPrice().multiply(joinDiscount.getDiscountNum()));
                                } else if (discountCate == 1 || discountCate == 2) {
                                    joinDiscount.setCurrentPrice(joinDiscount.getDiscountNum());
                                } else if (discountCate == 3) {
                                    joinDiscount.setCurrentPrice(joinDiscount.getDiscountNum());
                                }
                            }
                            joinDiscounts.sort(Comparator.comparing(ProdJoinDiscount::getCurrentPrice));
                            Integer discountCate = joinDiscounts.get(0).getDiscountCate();
                            prod.setKinds(1);
                            // 0折扣  1 一口价
                            if (discountCate == 0) {
                                prod.setCurrentPrice(prod.getProdPrice().multiply(joinDiscounts.get(0).getDiscountNum()));
                            } else if (discountCate == 1) {
                                prod.setCurrentPrice(joinDiscounts.get(0).getDiscountNum());
                            } else if (discountCate == 3) {
                                prod.setOneValue(true);
                                prod.setCurrentPrice(joinDiscounts.get(0).getDiscountNum());
                            }
                            prod.setBeauty(prod.getCurrentPrice());
                            prod.setDiscountImg(joinDiscounts.get(0).getDiscountImg());
                            Integer special = joinDiscounts.get(0).getSpecial();
                            if (special != null && special == 0) {
                                if (discountCate == 2) {
                                    prod.setCurrentPrice(joinDiscounts.get(0).getDiscountNum());
                                    prod.setOneValue(true);
                                }
                            }
                        }
                    }
                }
            }

        }
        return end;
    }
}
